package com.bdc.HBase;

import com.google.protobuf.ServiceException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import java.io.IOException;
import java.util.Properties;

public class HBaseUtils {
    /**
     * 建立连接
     *
     * @return
     */
    public static Connection getConnection() {
        try {
            //获取配置
            Configuration configuration = getConfiguration();
            //检查配置
            //HBaseAdmin.checkHBaseAvailable(configuration);
            return ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取配置
     *
     * @return
     */
    private static Configuration getConfiguration() {
        //Properties props = PropertiesLoaderUtils.loadAllProperties("hbase.properties");
        Properties props = new Properties(); //废弃
        String clientPort = props.getProperty("hbase.zookeeper.property.clientPort");
        String quorum = props.getProperty("hbase.zookeeper.quorum");

        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.property.clientPort", clientPort);
        config.set("hbase.zookeeper.quorum", quorum);
        return config;
    }
}
